package io.renren.modules.app.controller;

import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.form.RegisterForm;
import io.renren.modules.hospital.entity.EmrYsInfoEntity;
import io.renren.modules.hospital.service.EmrYsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/app")
@Api(value = "医生APP注册接口", tags = "医生App注册接口")
public class EmrAppRegisterController {
    @Autowired
    private EmrYsInfoService userService;

    @PostMapping("emr_register")
    @ApiOperation("注册")
    public R register(@RequestBody RegisterForm form) {
        //表单校验
        ValidatorUtils.validateEntity(form);

        EmrYsInfoEntity ysInfo=userService.queryByPhone(form.getMobile());
        if(ysInfo!=null){
            return R.ok("此手机号已被注册");
        }
        EmrYsInfoEntity user = new EmrYsInfoEntity();
        user.setEmrYsPhone(form.getMobile());
        user.setEmrYsName(form.getMobile());
        user.setEmrYsTitle("未知");
        user.setEmrYsSex("未知");
        user.setEmrYsSection("未知");
        user.setEmrYsPwd(DigestUtils.sha256Hex(form.getPassword()));
        user.setEmrYsCreate(new Date());
        userService.save(user);
        return R.ok();
    }
}
